<?php
/**
 * Created by PhpStorm.
 * User: stupboy
 * Date: 2018/2/11
 * Time: 11:34
 */

namespace app\api\controller;
use app\base\service;
use think\Db;

/**
 * Class Notify
 * 支付通知接收
 * @package app\api\controller
 */
class Notify
{
    public function wechat(){
        $notify = new service\WxNotify();
        $notify->Handle(true);
    }

    //接收微信支付回传数据by yulin
    public function check_wxpay() {
        $xmlData  = file_get_contents("php://input");
        libxml_disable_entity_loader(true);
        $data = json_decode(json_encode(simplexml_load_string($xmlData, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
//        Db::name('company_recharge')->where('company_recharge_id', $data['attach'])->update(['state' => '3']);
//        ksort($data);
//        $buff = '';
//        foreach ($data as $k => $v){
//            if($k != 'sign'){
//                $buff .= $k . '=' . $v . '&';
//            }
//        }
//        $stringSignTemp = $buff . 'key=192006250b4c09247ec02edce69f6a2d';//key为证书密钥
//        $sign = strtoupper(md5($stringSignTemp));
////判断算出的签名和通知信息的签名是否一致
//        if($sign == $data['sign']){
//            Db::name('company_recharge')->where('company_recharge_id', $data['attach'])->update(['state' => '3']);
//            //处理完成之后，告诉微信成功结果
//            echo '<xml>
//              <return_code><![CDATA[SUCCESS]]></return_code>
//              <return_msg><![CDATA[OK]]></return_msg>
//          </xml>';
//            exit();
//        }
        if($data['return_code'] == 'SUCCESS') {
            if($data['result_code'] == 'SUCCESS') {
                $car_use_recharge_return = Db::name('company_recharge')->where('company_recharge_id', $data['attach'])->find();

                if($car_use_recharge_return['state'] == '2') {

                    $company_return = Db::name('company')->where('company_id', $car_use_recharge_return['company_id'])->find();

                    $company_return['quota'] += $car_use_recharge_return['number'];

                    $company_return = Db::name('company')->where('company_id', $car_use_recharge_return['company_id'])->update(['quota' => $company_return['quota']]);

                }
                Db::name('company_recharge')->where('company_recharge_id', $data['attach'])->update(['state' => '3']);
                $xmldata="<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
                return $xmldata;
            }
        }
    }


}